'use strict'

const { TaiSpider } = require('tai-spider');

const keywords = [
    'Anticipated Re-entries'
];

const fieldNameMap = [{
    fn: 'nation',
    dn: 'Nation:',
}, {
    fn: 'type',
    dn: 'Type / Application:'
}, {
    fn: 'type',
    dn: 'Type, Application:'
}, {
    fn: 'operator',
    dn: 'Operator:'
}, {
    fn: 'contractors',
    dn: 'Contractors:',
}, {
    fn: 'equipment',
    dn: 'Equipment:',
}, {
    fn: 'configuration',
    dn: 'Configuration:',
}, {
    fn: 'propulsion',
    dn: 'Propulsion:',
}, {
    fn: 'power',
    dn: 'Power:',
}, {
    fn: 'lifetime',
    dn: 'Lifetime:',
}, {
    fn: 'mass',
    dn: 'Mass:',
}, {
    fn: 'orbit',
    dn: 'Orbit:',
}];

const satlistMap = [{
    dn: 'Satellite',
    fn: 'satellite'
}, {
    dn: 'COSPAR',
    fn: 'cospar',
}, {
    dn: 'Date',
    fn: 'date',
}, {
    dn: 'LS',
    fn: 'ls',
}, {
    dn: '_col4',
    fn: 'status',
}, {
    dn: 'Launch Vehicle',
    fn: 'launchVehicle',
}, {
    dn: 'Remarks',
    fn: 'remarks',
}]

class GunterSatSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        options.es.template.mappings = {}
        this.name = 'gunter-sat';
        this.debug = true;
        this.start_urls = ['https://space.skyrocket.de/directories/sat_c.htm'];
        // this.start_urls = ['https://space.skyrocket.de/doc_sdat/jupiter-1.htm', 'https://space.skyrocket.de/doc_sdat/msg-1.htm'];
        // this.start_urls = ['https://space.skyrocket.de/doc_sdat/msg-1.htm', 'https://space.skyrocket.de/doc_sdat/starone-c4.htm'];
        // this.start_urls = ['https://space.skyrocket.de/doc_sdat/move-2.htm', 'https://space.skyrocket.de/doc_sdat/nanoff.htm'];
    }

    *parse(response) {
        for (let ele of response.css('.country-list li')) {
            yield response.follow(ele.css('a'), this.parseCountry);
        }
    }

    *parseCountry(response) {
        for (let ele of response.css('table.index ul li')) {
            yield* response.follow_all(ele.css('a'), this.parseType);
        }
    }

    *parseType(response) {
        for (let ele of response.css('table.index ul li')) {
            yield* response.follow_all(ele.css('a'), this.parseSat);
        }
    }

    *parseSat(response) {
        let satname = response.css('.container h1').extract_first();

        let description = response.css('#satdescription >p').extract().join('\n');

        let imglist = []
        for (let imageEle of response.css('#satdescription #contimg img')) {
            imglist.push({
                url: response.absolute_url(imageEle.attr('src')),
                text: imageEle.next('p') ? imageEle.next('p').extract() : 'noname',
            });
            yield response.follow(imageEle.attr('src'), this.parseImage, {
                download: true,
                options: {
                    type: 'jpg',
                },
                extData: {
                    satname,
                }
            });
        };

        let satdataEle = response.css('#satdata').get(0);

        let records = satdataEle.parseTable({
            direct: 'vertical', //horizontal
            fieldNameMap
        });

        let satlistEle = response.css('#satlist').get(0);
        let satlist = satlistEle.parseTable({
            fieldNameMap: satlistMap,
        });

        for (let sat of satlist) {
            for (let record of records) {
                yield Object.assign({
                    index_name: 'tai-gunter-sat',
                    satname,
                    imglist,
                    description,
                    id: sat.cospar,
                    uri: response.getUri(),
                    ...sat,
                }, record);
            }
        }
    }

    *parseImage(response) {
        yield {
            index_name: 'tai-file',
            ...response.options.extData,
        }
    }
}

module.exports = GunterSatSpider;